package com.zjw.zy.utils.taskpool;

import android.os.Handler;
import android.os.Looper;

import java.util.concurrent.Executor;

/**
 * 主线程池
 */
public class MainExecutor implements Executor {

    private final Handler mHandler = new Handler(Looper.getMainLooper());

    private MainExecutor() {
    }

    private static final class MainExecutorHolder {
        public static final MainExecutor INSTANCE = new MainExecutor();
    }

    public static MainExecutor get() {
        return MainExecutorHolder.INSTANCE;
    }

    public Handler getMainHandler() {
        return mHandler;
    }

    @Override
    public void execute(Runnable command) {
        //主线程
        if (Looper.myLooper() == Looper.getMainLooper()) {
            command.run();
        } else {
            mHandler.post(command);
        }
    }
}